// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class Game extends cc.Component {

    @property
    Count: number = 5;

    @property(cc.Prefab)
    prefab: cc.Prefab = null;

    @property(cc.Node)
    nodeParent: cc.Node = null;

    private mEggs: cc.Node[] = [];
    // LIFE-CYCLE CALLBACKS:

    // onLoad () {}

    start () {

    }

    // update (dt) {}

    onClick(event, data){
        switch(data){
            case 'add':{
                this.addEggs();
                break;
            }
            case 'move':{
                this.moveEggs();
                break;
            }
            case 'stop':{
                this.stopMoveEggs();
                break;
            }
        }
    }

    addEggs(){
        if(this.Count <= 0){
            return;
        }
        this.mEggs = [];

        const N = 360 / this.Count;
        for(let i = 0; i < this.Count; i++){
            let egg = cc.instantiate(this.prefab);
            let js = egg.getComponent('Egg');
            js.setRadian(i * N * Math.PI / 180);
            js.updatePos();
            egg.parent = this.nodeParent;
            this.mEggs.push(egg);
        }
    }

    moveEggs(){
        for(let i = 0; i < this.mEggs.length; i++){
            this.mEggs[i].getComponent('Egg').setMove(true);
        }
    }

    stopMoveEggs(){
        for(let i = 0; i < this.mEggs.length; i++){
            this.mEggs[i].getComponent('Egg').setMove(false);
        }
    }
}
